<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: customers_bd.php 11 2013-09-21 11:43:11Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 11 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 11:43:11 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

$get_bd = $db->db_query("SELECT
							customers_id,
							customers_firstname,
							customers_lastname,
							customers_dob,
							DATE_FORMAT(customers_dob, '%d.%m.%Y') AS dob,
							(YEAR(CURDATE()) - YEAR(customers_dob)) - (IF(RIGHT(CURDATE(),5) < RIGHT(customers_dob,5),0,1)) AS bd
						FROM
							".TABLE_CUSTOMERS."
						WHERE
						    DATE_FORMAT(CURDATE(), '%d.%c.') = DATE_FORMAT(customers_dob, '%d.%c.')");


//Geburtstage innerhalb der nächsten 2 Wochen (BETA)
/*
$get_bd_2w = $db->db_query("SELECT
								customers_id,
								customers_firstname,
								customers_lastname,
								customers_dob,
								DATE_FORMAT(customers_dob, '%d.%m.%Y') AS dob,
								(YEAR(CURDATE()) - YEAR(customers_dob)) - (IF(RIGHT(CURDATE(),5) < RIGHT(customers_dob,5),0,1)) AS age
							FROM
								".TABLE_CUSTOMERS."
							WHERE
								DATE_FORMAT(CURDATE(), '%d.%c.') != DATE_FORMAT(customers_dob, '%d.%c.')
							AND(
									MONTH(NOW()) = MONTH(NOW() + INTERVAL 14 DAY) 
								AND
									DAY(NOW()) <= DAY(customers_dob) 
								AND
									DAY(NOW() + INTERVAL 14 DAY) >= DAY(customers_dob) 
								AND
									DATE_FORMAT(CURDATE(), '%d.%c.') != DATE_FORMAT(customers_dob, '%d.%c.')
							)OR(
									MONTH(NOW()) + 1 = MONTH(NOW() + INTERVAL 14 DAY) 
								AND
									(MONTH(customers_dob) = MONTH(NOW()) 
								OR 
									MONTH(customers_dob) = MONTH(NOW() + INTERVAL 14 DAY)) 
								AND
									(DAY(customers_dob) >= DAY(NOW()) 
								OR 
									DAY(customers_dob) <= DAY(NOW() + INTERVAL 14 DAY))
								AND
									DATE_FORMAT(CURDATE(), '%d.%c.') != DATE_FORMAT(customers_dob, '%d.%c.')
								AND
									DATE_FORMAT(customers_dob, '%d.%c.') > DATE_FORMAT(CURDATE(), '%d.%c.'))");
*/
$get_bd_2w = $db->db_query("SELECT 
								customers_id,
								customers_firstname,
								customers_lastname,
								customers_dob,
								DATE_FORMAT(customers_dob, '%d.%m.%Y') AS dob
							FROM 
								".TABLE_CUSTOMERS."
							");
/*
$get_bd_2w = $db->db_query("SELECT
								customers_id,
								customers_firstname,
								customers_lastname,
								customers_dob,
								DATE_FORMAT(customers_dob, '%d.%m.%Y') AS dob
							FROM
								".TABLE_CUSTOMERS."
							WHERE
								customers_dob BETWEEN CURDATE() AND (CURDATE() + INTERVAL 14 DAY)");
								*/
								
/*$get_bd_2w = $db->db_query_limit("SELECT 
										customers_id,
										customers_firstname,
										customers_lastname,
										DATE_FORMAT(customers_dob, '%d.%m.%Y') AS dob,
										YEAR(CURDATE()) - YEAR(customers_dob) AS age
									FROM 
										".TABLE_CUSTOMERS." 
									WHERE 
										DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(customers_dob, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(customers_dob, '00-%m-%d'))
									ORDER BY 
										customers_dob 
									DESC", 10);	*/	


/*customers_dob <= DATE_SUB((CURDATE() + INTERVAL 14 DAY), INTERVAL (YEAR(customers_dob)-YEAR(customers_dob)) YEAR)*/
								
if($get_bd->_numOfRows){
	?>
	<h2>Heute</h2>
	
	<table width="100%" class="dataTable">
			<tr>
				<td width="1px"><b>ID</b></td>
				<td width="1px"><b>Vorname</b></td>
				<td width="1px"><b>Nachname</b></td>
				<td width="1px"><b>customers_dob</b></td>
				<td width="1px" align="right"><b>Alter</b></td>
			</tr>
	<?php
	
	while(!$get_bd->EOF){
		echo'<tr>
				<td><a href='.href_link(FILENAME_CUSTOMERS, 'subsite=customers&cID='.$get_bd->fields['customers_id']).'>'.$get_bd->fields['customers_id'].'</a></td>
				<td> '.$get_bd->fields['customers_firstname'].'</td>
				<td> '.$get_bd->fields['customers_lastname'].'</td>
				<td> '.$get_bd->fields['dob'].'</td>
				<td align="right"> '.$get_bd->fields['age'].'</td>
			</tr>';
		$get_bd->MoveNext();
	}
	echo '</table><br />';
}
else
	echo '<table width="100%" class="dataTable"><tr><td><b>Heute</b> hat niemand Geburtstag.</td></tr></table><br/>';
	
if($get_bd_2w->_numOfRows){
	$i=0; 
	
	while(!$get_bd_2w->EOF) {
		$gebdat = strtotime($get_bd_2w->fields['customers_dob']);
		$gebjahr = date('Y',$gebdat); 
		$gebmonat = date('n',$gebdat); 
		$gebtag = date('j',$gebdat);
		$tag = date('d',$gebdat);
		$monat = date('m',$gebdat);
												
		$jetzt = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$geburt = mktime(0,0,0,$monat,$tag,$gebjahr);
		$alter = intval(($jetzt - $geburt) / (3600 * 24 * 365));

	  	if($gebmonat == date('n') && $gebtag > date('j')) { 
	    	$geb_bald[$i][0] = $get_bd_2w->fields['customers_firstname'];
	    	$geb_bald[$i][1] = $get_bd_2w->fields['customers_lastname'];
	    	$geb_bald[$i][2] = $get_bd_2w->fields['dob'];
	    	$geb_bald[$i][3] = $get_bd_2w->fields['customers_id'];
	    	$geb_bald[$i][4] = $alter;
	    	$i++;
	  	}
	  	$get_bd_2w->MoveNext();
	}
	echo '</table>';
	$anzahl = count($geb_bald);
}
if($anzahl > 0) { ?>
	<h2>In den n&auml;chsten 2 Wochen</h2>
	<table width="100%" class="dataTable">
			<tr>
				<td width="1px"><b>ID</b></td>
				<td width="1px"><b>Vorname</b></td>
				<td width="1px"><b>Nachname</b></td>
				<td width="1px"><b>Geburtstag</b></td>
				<td width="1px" align="right"><b>Alter</b></td>
			</tr>
	<?php
	for($i=0; $i<$anzahl; $i++) {
		$geb_bald_sort[$i][0] = $geb_bald[$i][0];
		$geb_bald_sort[$i][1] = $geb_bald[$i][1];
		$geb_bald_sort[$i][2] = $geb_bald[$i][2];
		$geb_bald_sort[$i][3] = $geb_bald[$i][3];
		$geb_bald_sort[$i][4] = $geb_bald[$i][4];
	}
	if ($anzahl > 0)	
		array_qsort($geb_bald_sort, 2); 
	
	for($i=0;$i<$anzahl;$i++) {
		for($a=0;$a<$anzahl;$a++) {
			if (($geb_bald_sort[$i][0] == $geb_bald[$a][0]) && ($geb_bald_sort[$i][1] == $geb_bald[$a][1]))
				break;
		}
		$geb_bald_sort[$i][2] = $geb_bald[$a][2];
	}
	
	for($i=0; $i<$anzahl; $i++) {
		echo'<tr>
				<td><a href='.href_link(FILENAME_CUSTOMERS, 'cID='.$geb_bald_sort[$i][3]).'>'.$geb_bald_sort[$i][3].'</a></td>
				<td> '.$geb_bald_sort[$i][0].'</td>
				<td> '.$geb_bald_sort[$i][1].'</td>
				<td> '.$geb_bald_sort[$i][2].'</td>
				<td align="right"> '.$geb_bald_sort[$i][4].'</td>
			</tr>';
		
	}
	unset($geb_bald);
	echo '</table>';
}
else
	echo '<table width="100%" class="dataTable"><tr><td><b>In den n&auml;chsten 2 Wochen</b> hat niemand Geburtstag.</td></tr></table>';
	
function array_qsort (&$array, $column=0, $order=SORT_ASC, $first=0, $last= -2) {
    $keys = array_keys($array);
    if($last == -2) $last = count($array) - 1;
    if($last > $first) {
        $alpha = $first;
        $omega = $last;
        $key_alpha = $keys[$alpha];
        $key_omega = $keys[$omega];
        $guess = $array[$key_alpha][$column];
      while($omega >= $alpha) {
      if($order == SORT_ASC) {
        while($array[$key_alpha][$column] < $guess) {$alpha++; $key_alpha =
        $keys[$alpha]; }
        while($array[$key_omega][$column] > $guess) {$omega--; $key_omega =
        $keys[$omega]; }
        } else {
        while($array[$key_alpha][$column] > $guess) {$alpha++; $key_alpha =
        $keys[$alpha]; }
        while($array[$key_omega][$column] < $guess) {$omega--; $key_omega =
        $keys[$omega]; }
      }
      if($alpha > $omega) break;
        $temporary = $array[$key_alpha];
        $array[$key_alpha] = $array[$key_omega]; $alpha++;
        $key_alpha = $keys[$alpha];
        $array[$key_omega] = $temporary; $omega--;
        $key_omega = $keys[$omega];
      }
      array_qsort ($array, $column, $order, $first, $omega);
      array_qsort ($array, $column, $order, $alpha, $last);
    }
}
?>